//
//  NSDate+Extention.swift
//  Z时间的处理
//
//  Created by 张洪毓 on 15/11/22.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

import Foundation

extension NSDate {
    
    //将字符串转为日期对象
    class func sinaDate(dateString: String) -> NSDate? {
        let df = NSDateFormatter()
        df.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        let date = df.dateFromString(dateString)
        return date
    }
    
    //将日期转换为字符串
    func fullDescription() -> String {
        let calendar = NSCalendar.currentCalendar()
        if calendar.isDateInToday(self) {
            
            let delta = Int(NSDate().timeIntervalSinceDate(self))
            if delta < 60 {
                return "刚刚"
            }else if delta < 60 * 60 {
                return "\(delta / 60)分钟前"
            }
            return "\(delta / 60 / 60)小时前"
 
        } else {
            
            var dateformatString = ""
            if calendar.isDateInYesterday(self) {
                dateformatString = "昨天 HH:mm"
            }else {
                let result = calendar.components(.Year, fromDate: self, toDate: NSDate(), options: [])
                if result.year == 0 {
                    dateformatString = "MM-dd HH:mm"
                }else {
                    dateformatString = "yyyy-MM-dd HH:mm"
                }
            }
            let df = NSDateFormatter()
            df.dateFormat = dateformatString
            return df.stringFromDate(self)
        }
    }
}